<?php
namespace Admin\Controller;
class FuturesOptionController extends AdminController{
	//页面加载和搜索
	public function index(){
		//状态查询
		if($_REQUEST['status']){
			if($_REQUEST['status']==1){
				$sql['status']=array('eq',1);
				$status=1;
			}elseif($_REQUEST['status']==2){
				$sql['status']=array('eq',0);
				$status=2;
			}else{
				$status=-1;
			}
		}
		else{
			$status=-1;
		}
		//名称查询
		if($_REQUEST['name']){
			$sql['name']=array('like',"%".trim($_REQUEST['name'])."%");
		}
		//代码查询
		if($_REQUEST['code']){
			$sql['code']=array('like',"%".trim($_REQUEST['code'])."%");
		}
		//时间查询
		if ($_REQUEST['create_time']) {
			$time=strtotime($_REQUEST['create_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['create_time'] = array('between',"$start_time,$end_time");
		}
		$list = $this->lists(M('FuturesOption'),$sql,'id desc');
		$this->assign('status',$status);
		$this->assign('list',$list);
		$this->display();
	}


	public function add(){
		if(IS_POST){
			$name=I("post.name");
			if(empty($name)){
				$this->error("期货名称不能为空");
			}
			$code=I("post.code");
			if(empty($code)){
				$this->error("期货代码不能为空");
			}
			$type=I("post.type");
			$status=I("post.status");
			$data=array(
				'name'=>trim($name,""),
				'code'=>trim($code,""),
				'status'=>$status,
				'create_time'=>time(),
				'type'=>$type
			);
			$Model=M("futures_option");
			$result=$Model->add($data);
			if($result){
				$this->success("期货添加成功", U("FuturesOption/index"));
			}else{
				$this->error("对不起,期货添加失败");
			}

		}
		$this->display();
	}

	public function edit(){
		if (IS_POST) {
			$id = I("post.id");
			$name = I("post.name");
			if (empty($name)) {
				$this->error("期货名称不能为空");
			}
			$code = I("post.code");
			if (empty($code)) {
				$this->error("期货代码不能为空");
			}
			$type=I("post.type");
			$status = I("post.status");
			$data = array(
					'name' =>trim($name,""),
					'code' =>trim($code,""),
					'status' => $status,
					'type'=>$type
			);
			$Model = M("futures_option");
			$result = $Model->where('id=' . $id)->save($data);
			if ($result !== false) {
				$this->success("修改成功", U("FuturesOption/index"));
			} else {
				$this->error("对不起,修改失败");
			}
		}
		$id = I("get.id");
		$Model = M("futures_option");
		$qiData = $Model->where('id='.$id)->find();
		$this->assign("qiData", $qiData);
		$this->display();
	}

	//删除功能
	public function del(){
		if(IS_POST){
			foreach($_POST['ids'] as $v){
				M('FuturesOption')->where('id='.$v)->delete();
			}
			$this->success('删除成功',U('FuturesOption/index'));die;
		}
		if(IS_GET){
			$id = $_GET['ids'];
			M('FuturesOption')->where('id='.$id)->delete();
			$this->success('删除成功',U('FuturesOption/index'));die;
		}
	}
}
?>